// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "Rectangle.h"

Rectangle::Rectangle(double s, const QString &name) :
    Item(name)
{
    createRectangle(s,s,s);
}

Rectangle::Rectangle(double width, double height, double depth, const QString &name) :
    Item(name)
{
    createRectangle(width, height, depth);
}

void Rectangle::onFrameChanged(double frame)
{
    Q_UNUSED(frame);
    /* FIXME complete the code */
    // (optional)
    // apply a transformation matrix on this item using setGLTransform()
//    setTransform(Matrix::rotationZ(frame/100.0));
    // and frame to make the cube move.
    /* STOP here */
}

void Rectangle::createRectangle(double w, double h, double d)
{
    /* FIXME complete the code */
    // NOTE : suggestion for naming variables :
    //        TLF = Top Left Front
    //        BRB = Bottom Right Back
    //        etc...

    // 1) Define the 8 edge points of the cube as vectors
    Vector tlf = Vector(-w/2, h/2, -d/2);
    Vector trf = Vector( w/2, h/2, -d/2);
    Vector blf = Vector( -w/2, -h/2, -d/2);
    Vector brf = Vector( w/2, -h/2, -d/2);

    Vector tlb = Vector( -w/2, h/2, d/2);
    Vector trb = Vector( w/2, h/2, d/2);
    Vector blb = Vector( -w/2, -h/2, d/2);
    Vector brb = Vector( w/2, -h/2, d/2);

    // 2) Define the normal vector for each of the 6 faces
    Vector vectorLeft = Vector(-1.0, 0.0, 0.0);
    Vector vectorRight = Vector(1.0, 0.0, 0.0);
    Vector vectorUp = Vector(0.0, 1.0, 0.0);
    Vector vectorDown = Vector(0.0, -1.0, 0.0);
    Vector vectorFront = Vector(0.0, 0.0, -1.0);
    Vector vectorBack = Vector(0.0, 0.0, 1.0);
    // 3) (optional) Define the texture vectors that will be applied on the cube
    //    This can be the same for each face (making the texture repeat on each face)
    //    or the texture can be 'wrapped' around the cube.

    // 4) Define the 6 faces of the cube (Use the Face class)
    //    Use Face::addPoint(point, normal, texture) to define each point of the face.
    //    You can use Face::setColor() to set the color for each face
    //    if glColorMaterial is enabled.
    Face faceLeft(Face::Quads);
    Face faceRight(Face::Quads);
    Face faceUp(Face::Quads);
    Face faceDown(Face::Quads);
    Face faceFront(Face::Quads);
    Face faceBack(Face::Quads);

    //test texture
    Vector t1 = Vector(0,0,0);
    Vector t2 = Vector(0,1,0);
    Vector t3 = Vector(1,1,0);
    Vector t4 = Vector(1,0,0);

    faceLeft.setColor(Qt::blue);
    faceLeft.addPoint(tlf, vectorLeft, t1);
    faceLeft.addPoint(blf, vectorLeft, t2);
    faceLeft.addPoint(blb, vectorLeft, t3);
    faceLeft.addPoint(tlb, vectorLeft, t4);

    faceRight.setColor(Qt::red);
    faceRight.addPoint(trf, vectorRight);
    faceRight.addPoint(brf, vectorRight);
    faceRight.addPoint(brb, vectorRight);
    faceRight.addPoint(trb, vectorRight);

    faceUp.setColor(Qt::yellow);
    faceUp.addPoint(tlf, vectorUp);
    faceUp.addPoint(tlb, vectorUp);
    faceUp.addPoint(trb, vectorUp);
    faceUp.addPoint(trf, vectorUp);

    faceDown.setColor(Qt::green);
    faceDown.addPoint(blf, vectorDown);
    faceDown.addPoint(blb, vectorDown);
    faceDown.addPoint(brb, vectorDown);
    faceDown.addPoint(brf, vectorDown);

    faceFront.setColor(Qt::cyan);
    faceFront.addPoint(tlf, vectorFront);
    faceFront.addPoint(trf, vectorFront);
    faceFront.addPoint(brf, vectorFront);
    faceFront.addPoint(blf, vectorFront);

    faceBack.setColor(Qt::magenta);
    faceBack.addPoint(tlb, vectorBack);
    faceBack.addPoint(trb, vectorBack);
    faceBack.addPoint(brb, vectorBack);
    faceBack.addPoint(blb, vectorBack);

    // 5) Add the 6 faces to this item using addFace()
    /* STOP here */
    addFace(faceLeft);
    addFace(faceUp);
    addFace(faceRight);
    addFace(faceDown);
    addFace(faceFront);
    addFace(faceBack);
}

